<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm">
    <el-form-item label="父字典" prop="deptCategory" v-if="ruleForm.parentId !== '0'">
      <el-select v-model="ruleForm.parentId" placeholder="请选择" style="width: 320px">
        <el-option
          v-for="item in deptTop"
          :key="item.id"
          :label="item.dictValue"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="字典编码" prop="code">
      <el-input  v-model="ruleForm.code" ></el-input>
    </el-form-item>
    <el-form-item label="字典键" prop="dictKey">
      <el-input  v-model="ruleForm.dictKey" ></el-input>
    </el-form-item>
    <el-form-item label="字典值" prop="dictValue">
      <el-input  v-model="ruleForm.dictValue" ></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input  v-model="ruleForm.remark" ></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="ruleForm.sort" :step="2"></el-input-number>
    </el-form-item>
    <el-form-item style="margin-top: 40px;margin-left: -28px">
      <el-button type="primary" @click="submitForm('ruleForm')" style="width: 320px;height: 41px;">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'DataUpdate',
  //组件参数
  props:{
    propsData:{
      type:Object,
      required:true
    },
    deptTree:{
      type:Array,
      required:true
    }
  },
  data(){
    return{
      ruleForm:{},
      loading:false,
      rules:{
        code: [
          { required: true, message: '请输入字典编码', trigger: 'blur' },
        ],
        dictKey: [
          { required: true, message: '请输入字典键', trigger: 'blur' },
        ],
        dictValue: [
          { required: true, message: '请输入字典值', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '请输入字典排序', trigger: 'blur' },
        ],
      },
      deptTop:[]
    }
  },
  mounted () {
    //转换数据，因为props中的属性是不可以修改的
    this.ruleForm = JSON.parse(JSON.stringify(this.propsData))
    this.getTopDict()
  },
  methods:{
    //提交数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post('/api/dictBiz/update',this.ruleForm).then(res => {
            this.loading = false
            ElMessage({
              type: 'success',
              showClose: true,
              message: res.data.message,
            })
            //触发事件
            this.$emit('tao')
          }).catch(() => this.loading = false)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //获取顶级字典
    getTopDict(){
      this.$http.get('/api/dictBiz/top').then(res =>{
        this.deptTop = res.data.data
      })
    }
  }
}
</script>

<style scoped>

</style>
